package com.barray.log_monitor;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import java.util.List;

public class LogPageActivity extends AppCompatActivity {
    private LogAdapter logAdapter;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        LogButtonManager.getInstance().removeButtonView();
        setContentView(R.layout.activity_log_page);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        //添加默认的返回图标
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //设置返回键可用
        getSupportActionBar().setHomeButtonEnabled(true);
        RecyclerView rvContent = findViewById(R.id.rv_content);
        logAdapter = new LogAdapter();
        rvContent.setAdapter(logAdapter);
    }

    @Override
    protected void onResume() {
        super.onResume();
        new Thread(){
            @Override
            public void run() {
                super.run();
                List<LogBean> logBeans = LogcatHelper.getInstance(LogPageActivity.this).getLogList();
                Log.d("-----------","size:"+(logBeans.size()));
                runOnUiThread(() -> logAdapter.setNewDatas(logBeans));
            }
        }.start();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_log_list, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if(item.getItemId() == android.R.id.home){
            finish();
        }else if(item.getItemId() == R.id.item_clear){
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    LogcatHelper.getInstance(LogPageActivity.this).clear();
                    runOnUiThread(() -> onResume());
                }
            }.start();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
//        LogButtonManager.getInstance().addButtonView(this);
    }
}